package com.ebpm.designer
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.containers.Canvas;

	/**
	 * 多选框 
	 **/
	public class SelectRect extends Canvas
	{
		public var workPanel:WorkPanel;
		public var nodeContainers:ArrayList;
		public var startMoveX:Number;
		public var startMoveY:Number;
		
		public function SelectRect(workPanel:WorkPanel)
		{
			this.workPanel = workPanel;
			this.nodeContainers = new ArrayList();
			this.initListener();
		}
		
		private function initListener():void {
			this.setStyle("backgroundColor", WF.SelectRectBgColor);
			this.setStyle("backgroundAlpha", 0.2);
			this.addEventListener(MouseEvent.MOUSE_OVER, _mouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, _mouseOut);
			this.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
			this.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
		}
		
		public function disVisible():void {
			this.x = 0;
			this.y = 0;
			this.width = 0;
			this.height = 0;
			WP.isMovingSelectRectByKeyboard = false;
		}
		
		public function _mouseOver(event:MouseEvent):void
		{
			this.cursorManager.setCursor(WF.Icon_Mouse_Move);
		}
		
		public function _mouseOut(event:MouseEvent):void
		{
			this.cursorManager.removeAllCursors();
		}
		
		public function _mouseUp(event:MouseEvent):void
		{
			WP.isMovingSelectRectEvent = false;
			WP.isDrawSelectRectEvent = false;
			this.disVisible();
			this.nodeContainers.removeAll();
		}
		
		public function select(sourceX:Number, sourceY:Number, targetX:Number, targetY:Number, isShowDir:Boolean = true):void {
			this.width = Math.abs(targetX - sourceX);
			this.height = Math.abs(targetY - sourceY);
			if(sourceX < targetX) {
				this.x = sourceX;
			} else {
				this.x = targetX;
			}
			if(sourceY < targetY) {
				this.y = sourceY;
			} else {
				this.y = targetY;
			}
			
			nodeContainers.removeAll();
			for(var i:int=0; i<this.workPanel.nodeContainers.length; i++) {
				var temp:NodeContainer = this.workPanel.nodeContainers.getItemAt(i) as NodeContainer;
				if(WF.intersects(this.x, this.y, this.width, this.height, temp.x, temp.y, temp.width, temp.height)) {
					if(isShowDir) temp.addDirs();	
					temp.startMoveX = temp.x;
					temp.startMoveY = temp.y;
					nodeContainers.addItem(temp);
				}
			}
			
			WP.isMovingSelectRectByKeyboard = true;
		}
		
		public function selectNodeContainers():void {
			var sourceX:Number = WP.mouseDownStageX;
			var sourceY:Number = WP.mouseDownStageY;
			var targetX:Number = WP.mouseMoveStageX;
			var targetY:Number = WP.mouseMoveStageY;
			this.select(sourceX, sourceY, targetX, targetY);
		}
		
		public function _mouseDown(event:MouseEvent):void
		{
			if(this.nodeContainers.length == 0) {
				this.disVisible();
				return ;
			}
			
			WP.isDrawSelectRectEvent = false;
			WP.isMovingSelectRectEvent = true;
			//记录当前 容器的x、y
			this.startMoveX = this.x;
			this.startMoveY = this.y;
		}
		
		public function movingNodeContainers():void {																			//移动【选择框】
			this.x = this.startMoveX + WP.mouseChangeX;
			this.y = this.startMoveY + WP.mouseChangeY;
			for(var i:int=0;i<this.nodeContainers.length;i++) {
				var temp:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				temp.movingBySelectRect(WP.mouseChangeX, WP.mouseChangeY);
			}
		}
		
		public function movingNodeContainersByKeyboard(changeX:Number=0, changeY:Number=0):void {
			this.x = this.x + changeX;
			this.y = this.y + changeY;
			for(var i:int=0;i<this.nodeContainers.length;i++) {
				var temp:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				temp.movingByKeyBoard(changeX, changeY);
			}
		}
		
		public function deleteNodeContainers():void {
			for(var i:int=0;i<this.nodeContainers.length;i++) {
				var temp:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				temp.deleteMySelf();
			}
			this.disVisible();
			this.nodeContainers.removeAll();
		}
		
		
		
		
		
		
		
		
		
		
	}
}